<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getSetRoleList, assignRoles } from '@/api/settings'
import { getUserDetail } from '@/api/user'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },

  methods: {
    async doSave() {
      try {
        const res = await assignRoles({ roleIds: [...this.roleIds], id: this.id })
        console.log('assignRoles', res)
        this.$message.success(res.message)
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log('assignRoles', err)
      }
    },
    // 保存
    hSubmit() {
      this.doSave()
    },
    // 获取所有角色列表
    async loadSetRoleList() {
      try {
        const res = await getSetRoleList()
        console.log('getSetRoleList', res)
        this.roleList = res.data.rows
        const info = await getUserDetail(this.id)
        console.log(info)
        this.roleIds = info.data.roleIds
        // 保存数据
      } catch (err) {
        console.log('getSetRoleList', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
